﻿// Created by Leestar54
// E-Mail:178078114@qq.com
using NativeWifi;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Wifi连接器.Common;

namespace Wifi连接器
{
    public partial class Frm_Main : Form
    {
        private List<Wlan.WlanAvailableNetwork> NetWorkList = new List<Wlan.WlanAvailableNetwork>();
        private WlanClient.WlanInterface wlanIface;

        public Frm_Main()
        {
            InitializeComponent();

        }

        /// <summary>
        /// 连接通知
        /// </summary>
        /// <param name="notifyData"></param>
        /// <param name="connNotifyData"></param>
        private void wlanIface_WlanConnectionNotification(Wlan.WlanNotificationData notifyData, Wlan.WlanConnectionNotificationData connNotifyData)
        {
            if (notifyData.notificationSource == NativeWifi.Wlan.WlanNotificationSource.MSM)
            {
                //这里是完成连接
                if ((NativeWifi.Wlan.WlanNotificationCodeMsm)notifyData.NotificationCode == NativeWifi.Wlan.WlanNotificationCodeMsm.Connected)
                {
                    Invoke(new Action(() =>
                    {
                        lblWifConnectState.Text = connNotifyData.profileName;
                    }));
                }
            }
            else if (notifyData.notificationSource == NativeWifi.Wlan.WlanNotificationSource.ACM)
            {
                //连接失败
                if ((NativeWifi.Wlan.WlanNotificationCodeAcm)notifyData.NotificationCode == NativeWifi.Wlan.WlanNotificationCodeAcm.ConnectionAttemptFail)
                {
                    wlanIface.DeleteProfile(connNotifyData.profileName);
                    Invoke(new Action(() =>
                    {
                        lblWifConnectState.Text = "未连接";
                        this.toolStripStatusLabel_state.Text = $"连接失败，请检查密码是否正确";
                       
                    }));
                    MessageBox.Show("连接失败，请检查密码是否正确");
                    
                    
                }
                if ((NativeWifi.Wlan.WlanNotificationCodeAcm)notifyData.NotificationCode == NativeWifi.Wlan.WlanNotificationCodeAcm.Disconnected)
                {
                    Invoke(new Action(() =>
                    {
                        lblWifConnectState.Text = "未连接";
                    }));
                }
                if ((NativeWifi.Wlan.WlanNotificationCodeAcm)notifyData.NotificationCode == NativeWifi.Wlan.WlanNotificationCodeAcm.Disconnecting)
                {
                    Invoke(new Action(() =>
                    {
                        lblWifConnectState.Text = "未连接";
                    }));
                }
                if ((NativeWifi.Wlan.WlanNotificationCodeAcm)notifyData.NotificationCode == NativeWifi.Wlan.WlanNotificationCodeAcm.ConnectionStart)
                {
                    Invoke(new Action(() =>
                    {
                        lblWifConnectState.Text = "连接中…";
                    }));
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            WlanClient client = new WlanClient();
            wlanIface = client.Interfaces[0];   //一般就一个网卡，有2个没试过。
            wlanIface.WlanConnectionNotification += wlanIface_WlanConnectionNotification;
            loadNetWork();
        }

        private void loadNetWork()
        {
            System.Int32 dwFlag = new Int32();
            Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0);
            foreach (Wlan.WlanAvailableNetwork network in networks)
            {
                string SSID = WlanHelper.GetStringForSSID(network.dot11Ssid);
                if (network.flags.HasFlag(Wlan.WlanAvailableNetworkFlags.Connected))
                {
                    lblWifConnectState.Text = SSID;
                }
                //如果有配置文件的SSID会重复出现。过滤掉
                if (!listBoxWife.Items.Contains(SSID))
                {
                    listBoxWife.Items.Add(SSID);
                    NetWorkList.Add(network);
                }
            }

            //信号强度排序
            NetWorkList.Sort(delegate(Wlan.WlanAvailableNetwork a, Wlan.WlanAvailableNetwork b)
            {
                return b.wlanSignalQuality.CompareTo(a.wlanSignalQuality);
            });
            listBoxWife.Items.Clear();
            foreach (Wlan.WlanAvailableNetwork network in NetWorkList)
            {
                listBoxWife.Items.Add(WlanHelper.GetStringForSSID(network.dot11Ssid));
            }
        }


        Wlan.WlanAvailableNetwork current_wn;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConnect_Click(object sender, EventArgs e)
        {
            
            current_wn = NetWorkList[listBoxWife.SelectedIndex];
            if (current_wn.securityEnabled && !WlanHelper.HasProfile(wlanIface, WlanHelper.GetStringForSSID(current_wn.dot11Ssid)))
            {
                Frm_Password fp = new Frm_Password();
                if (fp.ShowDialog() == DialogResult.OK)
                {
                    string pwd = fp.Password;

                    WlanHelper.ConnetWifi(wlanIface, current_wn, pwd);
                    fp.Dispose();
                }
            }
            else
            {
                WlanHelper.ConnetWifi(wlanIface, current_wn);
            }

        }

        private void listBoxWife_SelectedIndexChanged(object sender, EventArgs e)
        {
            Wlan.WlanAvailableNetwork wn = NetWorkList[listBoxWife.SelectedIndex];
            toolTip1.SetToolTip(listBoxWife, WlanHelper.GetWifiToolTip(wn));
        }

        private void lblReflush_Click(object sender, EventArgs e)
        {
            NetWorkList.Clear();
            listBoxWife.Items.Clear();
            loadNetWork();
        }
    }
}
